//
//  WebViewController.m
//  LINNER
//
//  Created by wei wang on 2017/4/11.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "WebViewController.h"
#import "Masonry.h"
@interface WebViewController ()<UIWebViewDelegate>
@property (strong, nonatomic)  UIWebView *webView;
@property (strong, nonatomic)  UILabel *tipsLabel;

@end

@implementation WebViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.webView = [[UIWebView alloc] init];
    [self.view addSubview:self.webView];
    self.webView.delegate = self;
    [self masLayoutSubview];
    
    
    [self setNavigationBarTitle:MyNSLocalizedString(@"Linner官网")];//done
   
    [self.navigationController.navigationBar makeTransparent_nornav];

    [self setLeftButtonImage:[UIImage imageNamed:@"tittle_back_nor"]];
//    [SVProgressHUD showWithStatus:MyNSLocalizedString(@"正在努力加载中...")];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [SVProgressHUD dismiss];
//    });
    
    NSString *saveLan = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject];;
        saveLan = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject];
        if ([saveLan isEqualToString:@"zh-Hans"]||[saveLan isEqualToString:@"zh-Hans-CN"]) {
            
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.linner.com.cn"]]];

        }else{
            
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.linner.io"]]];

        }
}


- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    
    [self.webView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(superView.mas_top).mas_offset(64);
        make.left.mas_equalTo(superView.mas_left);
        make.right.mas_equalTo(superView.mas_right);
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(superView);
    }];
    
    [self.view bringSubviewToFront:self.tipsLabel];
}

- (void)onLeftButtonClick:(id)sender{
    
    
    if ([self.webView canGoBack] == YES) {
        [self.webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


- (void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:animated];
    
}


- (void)dealloc{
    
    [self.webView removeFromSuperview];

    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [SVProgressHUD showErrorWithStatus:@"加载失败，请检查网络"];
    self.tipsLabel.text = MyNSLocalizedString(@"加载失败，请检查网络");
}



- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    if (self.tabBarController.selectedIndex == 1) {
//
//    }else{
//        return;
//    }
    
    self.tipsLabel.hidden = YES;
//    [SVProgressHUD dismiss];
}

- (UILabel *)tipsLabel
{
    if(!_tipsLabel)
    {
        UIView *superView = self.view;
        _tipsLabel = [[UILabel alloc]init];
        [superView addSubview:_tipsLabel];
        [_tipsLabel setFont:[UIFont systemFontOfSize:15]];
        [_tipsLabel setTextColor:[UIColor lightGrayColor]];
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel.text = MyNSLocalizedString(@"正在加载中...");
    }
    return _tipsLabel;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
